﻿using AutoMapper;
using B.Z.BasicData.Domain.Entity.Excused;
using B.Z.BasicData.ErrorCode;
using B.Z.BasicData.Infrastructure;
using MediatR;

namespace B.Z.BasicData.API.Write.Application.ExcusedManage
{
    public class ExcuseAddCommandHandler : IRequestHandler<ExcuseAddCommand, APIResult<int>>
    {
        private readonly IBaseRepository<T_Excused> excusedReposotory;
        private readonly IMapper mapper;

        public ExcuseAddCommandHandler(IBaseRepository<T_Excused> excusedReposotory, IMapper mapper)
        {
            this.excusedReposotory = excusedReposotory;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(ExcuseAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code = APIEnum.ok;
            result.Message = "保存成功";
            var model = mapper.Map<T_Excused>(request);
            if (model.Name == model.LeaderName) 
            {
                model.Status = 1;
                model.Remark = 1;
                model.CreateTime = DateTime.Now;
                model.UpdateTime = model.CreateTime;
            }
            result.Data = await excusedReposotory.Add(model);
            return await Task.FromResult(result);
        }
    }
}
